package ciel.annotator;

import org.mt4j.AbstractMTApplication;
import org.mt4j.components.TransformSpace;
import org.mt4j.components.visibleComponents.shapes.AbstractShape;
import org.mt4j.components.visibleComponents.shapes.MTRectangle;
import org.mt4j.components.visibleComponents.widgets.MTImage;
import org.mt4j.components.visibleComponents.widgets.MTTextArea;
import org.mt4j.components.visibleComponents.widgets.MTTextArea.ExpandDirection;
import org.mt4j.components.visibleComponents.widgets.keyboard.MTKeyboard;
import org.mt4j.input.gestureAction.InertiaDragAction;
import org.mt4j.input.inputProcessors.componentProcessors.AbstractComponentProcessor;
import org.mt4j.input.inputProcessors.componentProcessors.dragProcessor.DragProcessor;
import org.mt4j.input.inputProcessors.componentProcessors.rotateProcessor.RotateProcessor;
import org.mt4j.input.inputProcessors.componentProcessors.scaleProcessor.ScaleProcessor;
import org.mt4j.util.MTColor;
import org.mt4j.util.font.FontManager;
import org.mt4j.util.math.Vector3D;
import org.mt4j.util.math.Vertex;

import processing.core.PApplet;
import processing.core.PGraphics;
import ciel.res.CielSettings;
import ciel.scene.CielImageScene;

public class CielAnnotator extends MTRectangle{
	
	private MTImage image;
	private PApplet pApplet;
	private String filename;
	private CielImageScene scene;
	private final MTTextArea notes;

	public CielAnnotator(PApplet pApplet, Vertex upperLeft, float width, float height, String filename, CielImageScene scene) {
		super(pApplet, upperLeft, width, height);
		
		this.pApplet = pApplet;
		this.filename = filename;
		this.scene = scene;
		
		MTImage tmpImage = new MTImage(pApplet, pApplet.loadImage(CielSettings.Filesystem.IMAGEDIR + AbstractMTApplication.separator + filename));
		image = tmpImage;
		
		notes = new MTTextArea(pApplet, FontManager.getInstance().createFont(pApplet, "arial.ttf", 50, MTColor.WHITE)) {
			private int count = 0;
			
			@Override
			public synchronized void appendCharByUnicode(String unicode) {
				// TODO Auto-generated method stub
				super.appendCharByUnicode(unicode);
				count++;
				if(count > 25) {
					count = 0;
					appendCharByUnicode("\n");
				}
			}
		};
		

		addChild(notes);
		
		initUI();
		
	}
	
	public void initModuls() {
		final String name = ((String)getUserData("name"));
		notes.setExpandDirection(ExpandDirection.UP);
		notes.setNoStroke(true);
		notes.setNoFill(true);
		notes.setDrawSmooth(true);
		notes.unregisterAllInputProcessors();
		notes.setEnableCaret(true);
		notes.setText(scene.getAnnotation(filename));
		
		final MTKeyboard key = new MTKeyboard(pApplet) {
			@Override
			public void close() {
				// TODO Auto-generated method stub
				scene.removeAnnotator(filename, name);
				super.close();
			}
			
			@Override
			public void save() {
				// TODO Auto-generated method stub
				scene.setAnnotation(filename, notes.getText());
				scene.removeAnnotator(filename, name);
				super.save();
			}
		};
	    key.setWidthXYGlobal((float) (getWidthXYGlobal()*0.8));
	    key.snapToKeyboard(notes);
	    key.addTextInputListener(notes);
	    key.setNoStroke(true);
	    key.setFillColor(new MTColor(255,255,255,0));
	    addChild(key);
	    
	    for (AbstractComponentProcessor ip : key.getInputProcessors()){
			if (ip instanceof ScaleProcessor) {
				key.unregisterInputProcessor(ip);
			}
			if (ip instanceof DragProcessor) {
				key.unregisterInputProcessor(ip);
			}
			if (ip instanceof RotateProcessor) {
				key.unregisterInputProcessor(ip);
			}
		}
		key.removeAllGestureEventListeners(ScaleProcessor.class);
		key.removeAllGestureEventListeners(DragProcessor.class);
		key.removeAllGestureEventListeners(RotateProcessor.class);
	    
	    float x = getPosition(TransformSpace.GLOBAL).x;
	    float y = getPosition(TransformSpace.GLOBAL).y;
	    
	    key.setPositionGlobal(new Vector3D(x,y));
	}
	
	private void initUI() {
		setFillColor(new MTColor(125, 125, 125, 100));
		setNoStroke(true);
		
		setBoundsBehaviour(AbstractShape.BOUNDS_ONLY_CHECK );
		addGestureListener(DragProcessor.class, new InertiaDragAction(300, .97f, 17));
		
		addChild(image);
		image.setWidthXYRelativeToParent(getWidthXYGlobal()/5);
		image.removeAllGestureEventListeners();
		image.setHeightXYGlobal(getHeightXYGlobal());
		float imageX = getPosition(TransformSpace.GLOBAL).getX() - getWidthXY(TransformSpace.GLOBAL)/2 - image.getWidthXY(TransformSpace.GLOBAL)/2;
		float imageY = getPosition(TransformSpace.GLOBAL).getY();
		image.setAnchor(PositionAnchor.CENTER);
		image.setPositionGlobal(new Vector3D(imageX, imageY));
	}
	
	public void drawComponent(PGraphics g) {
		super.drawComponent(g);
		Vector3D tmpVector = getCenterPointGlobal();
		if(tmpVector.x >= CielSettings.Dimensions.SCREEN_WIDTH) {
			setPositionGlobal(new Vector3D(0, tmpVector.y));
		} else if (tmpVector.x <= 0) {
			setPositionGlobal(new Vector3D(CielSettings.Dimensions.SCREEN_WIDTH, tmpVector.y));
		}
		if(tmpVector.y >= CielSettings.Dimensions.SCREEN_HEIGHT) {
			setPositionGlobal(new Vector3D(tmpVector.x, 0));
		} else if (tmpVector.y <= 0) {
			setPositionGlobal(new Vector3D(tmpVector.x, CielSettings.Dimensions.SCREEN_HEIGHT));
		}
	}


	
}
